﻿<UserControl x:Class="Snipps.Views.FilterView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:Command="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WPF45"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:Vm="clr-namespace:Snipps.ViewModels" xmlns:Behaviors="clr-namespace:Snipps.Infrastructure.Behaviors"
             xmlns:Controls="clr-namespace:Snipps.Controls" xmlns:Langs="clr-namespace:Snipps.Langs"
             xmlns:Models="clr-namespace:Snipps.Core.Entities"
             Name="us1"
             d:DesignHeight="300"
             d:DesignWidth="300"
             mc:Ignorable="d">
    <UserControl.Resources>

        <ResourceDictionary>

            <BooleanToVisibilityConverter x:Key="boolToVisibilityConverter" />

            <Style x:Key="PreviewContentTextBoxStyle"
                   BasedOn="{StaticResource ContentTextBoxStyle}"
                   TargetType="TextBox">
                <Setter Property="IsReadOnly" Value="True" />
                <Setter Property="BorderBrush" Value="{StaticResource ForegroundBrush}" />
                <Setter Property="BorderThickness" Value="0,1,0,1" />
            </Style>

        </ResourceDictionary>
    </UserControl.Resources>

    <UserControl.InputBindings>
        <KeyBinding Key="Enter"
                    Command="{Binding CurrentFilter.CompletionList.ActivateSelectedItemCommand}" />
        <KeyBinding Key="D"
                    Command="{Binding CurrentFilter.CompletionList.RemoveSelectedItemCommand}"
                    Modifiers="Control" />
        <KeyBinding Key="g"
                    Command="{Binding CurrentFilter.CompletionList.SelectFirstItemCommand}"
                    Modifiers="Control" />
        <KeyBinding Key="g"
                    Command="{Binding CurrentFilter.CompletionList.SelectLastItemCommand}"
                    Modifiers="Ctrl+Shift" />
        <KeyBinding Key="Down"
                    Command="{Binding CurrentFilter.CompletionList.SelectNextItemCommand}" />
        <KeyBinding Key="Up"
                    Command="{Binding CurrentFilter.CompletionList.SelectPrevItemCommand}" />
        <KeyBinding Key="N"
                    Command="{Binding CurrentFilter.CompletionList.SelectNextItemCommand}"
                    Modifiers="Control" />
        <KeyBinding Key="P"
                    Command="{Binding CurrentFilter.CompletionList.SelectPrevItemCommand}"
                    Modifiers="Control" />
        <KeyBinding Key="E"
                    Command="{Binding CurrentFilter.CompletionList.EditSelectedItemCommand}"
                    Modifiers="Control" />
        <KeyBinding Key="I"
                    Command="{Binding ToggleSnippetContentVisibilityCommand}"
                    Modifiers="Control" />
        <KeyBinding Key="Escape"
                    Command="{x:Static Vm:WindowCommands.HideCommand}" />
    </UserControl.InputBindings>

    <i:Interaction.Behaviors>
        <Behaviors:ExecCmdFromInputBindingsOnPreviewKeyDownBahavior />
    </i:Interaction.Behaviors>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition x:Name="row1"
                           Height="*"
                           MinHeight="100" />
            <RowDefinition x:Name="row2"
                           Height="2*">
                <RowDefinition.Style>
                    <Style TargetType="{x:Type RowDefinition}">
                        <Setter Property="Height" Value="3*" />
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding IsSnippetDetailsVisible}"
                                         Value="False">
                                <Setter Property="Height" Value="0" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </RowDefinition.Style>
            </RowDefinition>
        </Grid.RowDefinitions>

        <StackPanel Grid.Row="0">
            <Controls:BusyTextBox IsSearching="{Binding SnippetFilter.IsBusy}"
                                  Text="{Binding SnippetFilter.FilterText,
                                                 UpdateSourceTrigger=PropertyChanged}"
                                  WatermarkText="{x:Static Langs:Lang.WatermarkSnippetName}">
                <i:Interaction.Behaviors>
                    <Behaviors:BindableFocusBehavior FocusProperty="{Binding SnippetFilter.IsActive, Mode=TwoWay}" />
                </i:Interaction.Behaviors>
            </Controls:BusyTextBox>

            <StackPanel Margin="6,2,0,2">


                <Controls:BusyTextBox IsSearching="{Binding ProgrammingLangFilter.IsBusy}"
                                      Text="{Binding ProgrammingLangFilter.FilterText,
                                                     UpdateSourceTrigger=PropertyChanged}"
                                      WatermarkText="{x:Static Langs:Lang.WatermarkProgrammingLang}">
                    <i:Interaction.Behaviors>
                        <Behaviors:BindableFocusBehavior FocusProperty="{Binding ProgrammingLangFilter.IsActive, Mode=TwoWay}" />
                    </i:Interaction.Behaviors>

                </Controls:BusyTextBox>

                <Controls:TagsTextBox DisplayValueProperty="Name"
                                      ObjectType="{x:Type Models:Tag}"
                                      Tags="{Binding TagFilter.FilterTags}"
                                      Text="{Binding TagFilter.FilterText,
                                                     Mode=TwoWay,
                                                     UpdateSourceTrigger=PropertyChanged}"
                                      WatermarkText="{x:Static Langs:Lang.WatermarkTags}">

                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="RemoveItem">
                            <Command:EventToCommand Command="{Binding TagFilter.RemoveItemCommand}"
                                                    PassEventArgsToCommand="True" />
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                    <i:Interaction.Behaviors>
                        <Behaviors:BindableFocusBehavior FocusProperty="{Binding TagFilter.IsActive, Mode=TwoWay}" />
                    </i:Interaction.Behaviors>
                </Controls:TagsTextBox>
            </StackPanel>
        </StackPanel>

        <Separator Grid.Row="1" />

        <ContentPresenter Grid.Row="2"
                          Content="{Binding CurrentFilter.CompletionList}" />

        <TextBox x:Name="snippetDetailsControl"
                 Grid.Row="3"
                 Style="{StaticResource PreviewContentTextBoxStyle}"
                 Visibility="{Binding IsSnippetDetailsVisible,
                                      Converter={StaticResource boolToVisibilityConverter}}">
            <TextBox.Text>
                <PriorityBinding FallbackValue="Loading ...">
                    <Binding IsAsync="True"
                             Path="CurrentSnippetContent" />
                    <Binding Path="CurrentSnippetContentFast" />
                </PriorityBinding>
            </TextBox.Text>
        </TextBox>


    </Grid>
</UserControl>